package com.zjc.yohoshop.mvp.view.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.HomeMenuEntity;
import com.zjc.yohoshop.di.compont.DaggerClassifyFragmentCompont;
import com.zjc.yohoshop.di.modules.ClassifyFragmentModules;
import com.zjc.yohoshop.eventbus_bean.BaseEventBusEntity;
import com.zjc.yohoshop.eventbus_bean.OpenEventBus;
import com.zjc.yohoshop.eventbus_bean.SelectCategoryRadioButton;
import com.zjc.yohoshop.mvp.contract.ClassifyFragmentContract;
import com.zjc.yohoshop.mvp.persenter.ClassifyFragmentPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 分类Fragment包含品类、品牌
 */
public class ClassifyFragment extends BaseFragment<ClassifyFragmentPresenter> implements ClassifyFragmentContract.ClassiftFragmentIView, View.OnClickListener {

    private View view;
    private ImageView iv_classify_fragment_sliding_menu_left, iv_classify_category, iv_classify_brand;
    private RadioGroup rg_classify;
    private RadioGroup rg_classify_tab;
    private FragmentManager manager;
    private CategoryFragment categoryFragment;
    private BrandFragment brandFragment;

    private List<HomeMenuEntity.Values> values;


    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_classify, null);
        return view;
    }

    @Override
    public void init(View view) {
        EventBus.getDefault().register(this);
        manager = getActivity().getSupportFragmentManager();
        categoryFragment = new CategoryFragment();
        brandFragment = new BrandFragment();
        iv_classify_fragment_sliding_menu_left = view.findViewById(R.id.iv_classify_fragment_sliding_menu_left);
        rg_classify = view.findViewById(R.id.rg_classify);
        rg_classify_tab = view.findViewById(R.id.rg_classify_tab);
        iv_classify_category = view.findViewById(R.id.iv_classify_category);
        iv_classify_brand = view.findViewById(R.id.iv_classify_brand);

    }

    @Override
    public void initData() {
        p.requestMenu();
        addAll();
    }

    @Override
    public void setupDagger() {
        DaggerClassifyFragmentCompont.builder().classifyFragmentModules(new ClassifyFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        iv_classify_fragment_sliding_menu_left.setOnClickListener(this);

        //顶部标题单选按钮组监听
        rg_classify.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_classify_category:
                        iv_classify_category.setVisibility(View.VISIBLE);
                        iv_classify_brand.setVisibility(View.INVISIBLE);
                        showFrag(categoryFragment);
                        break;
                    case R.id.rb_classify_brand:
                        iv_classify_category.setVisibility(View.INVISIBLE);
                        iv_classify_brand.setVisibility(View.VISIBLE);
                        showFrag(brandFragment);
                        break;
                }
            }
        });
        //顶部tab单选按钮组监听
        rg_classify_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_classify_men:
                        Toast.makeText(getActivity(), "Men", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_classify_women:
                        Toast.makeText(getActivity(), "Women -> 没数据", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_classify_kids:
                        Toast.makeText(getActivity(), "Kids -> 没数据", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_classify_blk:
                        Toast.makeText(getActivity(), "BLK -> 没数据", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        rg_classify.check(R.id.rb_classify_category);//标题默认选中
        rg_classify_tab.check(R.id.rb_classify_men);//头部tab默认选中
    }

    @Override
    public void menuSuccess(HomeMenuEntity entity) {
        Log.e("ZJC", "ClassifyFragment -> menuSuccess: -> " + entity.values.size());
        values = entity.values;
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_classify_fragment_sliding_menu_left://打开侧滑菜单
                EventBus.getDefault().post(new OpenEventBus(BaseEventBusEntity.OPENMENU));
                break;
        }
    }


    /**
     * 显示指定的fragment
     */
    private void showFrag(Fragment fragment){
        hideAll();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }

    /**
     * 隐藏所有Fragment
     */
    private void  hideAll(){
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(categoryFragment);
        transaction.hide(brandFragment);
        transaction.commit();
    }

    /**
     * 添加所有Fragment
     */
    private void addAll(){
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_classify, categoryFragment);
        transaction.add(R.id.fl_classify, brandFragment);
        transaction.commit();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(SelectCategoryRadioButton entity){
        if (entity.type == BaseEventBusEntity.CATEGORYRADIO){
            if ("MAN".equals(entity.name)){
                rg_classify_tab.check(R.id.rb_classify_men);
            }else if ("WOMAN".equals(entity.name)){
                rg_classify_tab.check(R.id.rb_classify_women);
            }else if ("KIDS".equals(entity.name)){
                rg_classify_tab.check(R.id.rb_classify_kids);
            }else if ("LIFE".equals(entity.name)){
                rg_classify_tab.check(R.id.rb_classify_blk);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
